package web.action;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import web.handle.NotificationBean;
import web.layout.NotificationInput;
import web.layout.NotificationOneTimeCheckbox;

import ejb.entity.Notification;
import ejb.manager.NotificationManager;

@ManagedBean
@ViewScoped
public class NotificationAction {
	@EJB
	private NotificationManager notificationManager;	
	
	@ManagedProperty(value="#{notificationBean}")
	private NotificationBean notificationBean;
	
	@ManagedProperty(value="#{notificationInput}")
	private NotificationInput notificationInput;
	
	@ManagedProperty(value="#{notificationOneTimeCheckbox}")
	private NotificationOneTimeCheckbox notificationOneTimeCheckbox;
	
	public NotificationAction(){}
	
	public NotificationBean getNotificationBean() {
		return notificationBean;
	}

	public void setNotificationBean(NotificationBean notificationBean) {
		this.notificationBean = notificationBean;
	}

	public NotificationInput getNotificationInput() {
		return notificationInput;
	}

	public void setNotificationInput(NotificationInput notificationInput) {
		this.notificationInput = notificationInput;
	}

	public NotificationOneTimeCheckbox getNotificationOneTimeCheckbox() {
		return notificationOneTimeCheckbox;
	}

	public void setNotificationOneTimeCheckbox(NotificationOneTimeCheckbox notificationOneTimeCheckbox) {
		this.notificationOneTimeCheckbox = notificationOneTimeCheckbox;
	}

	public String addNotification(){
		
		if(this.getNotificationInput().getMinTempInput().isDisabled()) this.getNotificationBean().setMintemp(-65);
		if(this.getNotificationInput().getMaxTempInput().isDisabled()) this.getNotificationBean().setMaxtemp(-65);
		if(this.getNotificationInput().getMinPressureInput().isDisabled()) this.getNotificationBean().setMinpressure(-65);
		if(this.getNotificationInput().getMaxPressureInput().isDisabled()) this.getNotificationBean().setMaxpressure(-65);
		
		System.out.println("Aura: " + this.getNotificationBean().getType());
		boolean oneTime = this.getNotificationOneTimeCheckbox().getOneTimeCheckbox().isSelected();
		Notification newNotification = notificationManager.addNotification(this.getNotificationBean().getMessage(), this.getNotificationBean().getType(), this.getNotificationBean().getMintemp(), this.getNotificationBean().getMaxtemp(), this.getNotificationBean().getMinpressure(), this.getNotificationBean().getMaxpressure(), this.getNotificationBean().getId(), oneTime);
		
		this.getNotificationBean().setMintemp(0);
		this.getNotificationBean().setMaxtemp(0);
		this.getNotificationBean().setMinpressure(1000);
		this.getNotificationBean().setMaxpressure(1000);
		
		if(newNotification == null) return "error";
		else return null;
	}
	
	public String deleteNotification(){
		String tmp = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("deleteID");
		Integer id = Integer.parseInt(tmp);
		boolean result = notificationManager.deleteNotification(id);
		
		if(result) return null;
		else return "error";
	}
}
